package com.winit.datasync.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.winit.datasync.dao.UpsWarehouseDao;
import com.winit.datasync.entity.UpsWarehouse;
import com.winit.datasync.utils.ConstantUtils;
import com.winit.datasync.utils.DateUtil;

/**
 * 存储仓配置
 * 
 * @author liangming.deng
 *
 */
@Repository("upsWarehouseDao")
public class UpsWarehouseDaoImpl implements UpsWarehouseDao {

	@Autowired
	private CassandraTemplate cassandraTemplate;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Autowired
	private MongoTemplate mongoTemplate;

	@Override
	public void batchInsert(final List<UpsWarehouse> list, int dataSourceType) {
		if (list == null || list.size() == 0)
			return;
		if (3 == dataSourceType) {
			return;
		} else if (1 == dataSourceType) {
			jdbcTemplate.batchUpdate(ConstantUtils.ORACLE_BATCH_INSERT_UPS_WAREHOUSE,
					new BatchPreparedStatementSetter() {

						@Override
						public void setValues(PreparedStatement ps, int index) throws SQLException {
							UpsWarehouse upsWarehouse = list.get(index);
							ps.setString(1, upsWarehouse.getCode());
							ps.setLong(2, upsWarehouse.getSales_days());
							ps.setString(3, upsWarehouse.getBj_date_of_cal_store());
							ps.setString(4, upsWarehouse.getCity());
							ps.setString(5, upsWarehouse.getCity_code());
							ps.setString(6, upsWarehouse.getCountry());
							ps.setString(7, upsWarehouse.getCountry_code());
							ps.setTimestamp(8, DateUtil
									.convertUtilDateToSqlTimestamp(upsWarehouse.getCreated()));
							ps.setString(9, upsWarehouse.getCreatedBy());
							ps.setString(10, upsWarehouse.getIs_active());
							ps.setString(11, upsWarehouse.getName());
							ps.setLong(12, upsWarehouse.getOrg_id());
							ps.setString(13, upsWarehouse.getRegion_code());
							ps.setString(14, upsWarehouse.getRegion_name());
							ps.setString(15, upsWarehouse.getState());
							ps.setTimestamp(16, DateUtil
									.convertUtilDateToSqlTimestamp(upsWarehouse.getUpdated()));
							ps.setString(17, upsWarehouse.getUpdatedby());
							ps.setString(18, upsWarehouse.getWarehouse_group());
							ps.setLong(19, upsWarehouse.getWarehouse_id());
							ps.setString(20, upsWarehouse.getNewtime_zone());
						}

						@Override
						public int getBatchSize() {
							return list.size();
						}
					});
		} else {
			mongoTemplate.insert(list, UpsWarehouse.class);
		}
	}

	@Override
	public void delete(UpsWarehouse upsWarehouse, int dataSourceType) {
		if (3 == dataSourceType) {
			return;
		} else if (1 == dataSourceType) {
//			jdbcTemplate.execute(ConstantUtils.ORACLE_DELETE_UPS_WAREHOUSE);
		} else {
			Query query = new Query();
			query.addCriteria(Criteria.where("code").is(upsWarehouse.getCode()));

			mongoTemplate.remove(query, UpsWarehouse.class);

		}
	}

	@Override
	public List<UpsWarehouse> getByParams(HashMap<String, String> params, int dataSourceType) {

		StringBuilder sBuilder = new StringBuilder(ConstantUtils.ORACLE_GET_UPS_WAREHOUSE);
		if (params != null && !params.isEmpty()) {
			sBuilder.append(ConstantUtils.WHERE);

			// 拼接
			for (String key : params.keySet()) {
				sBuilder.append(StringUtils.EMPTY).append(key).append(ConstantUtils.EQUAL)
						.append(params.get(key));
			}

		}

		if (3 == dataSourceType) {
			return cassandraTemplate.select(sBuilder.toString(), UpsWarehouse.class);
		} else if (1 == dataSourceType) {
			return null;
		} else {
			return null;
		}

	}

	@Override
	public List<UpsWarehouse> getAll(int dataSourceType) {
		if (3 == dataSourceType) {
			return cassandraTemplate.select(ConstantUtils.CASSANDRA_GET_UPS_WAREHOUSE,
					UpsWarehouse.class);
		} else if (1 == dataSourceType) {
			return jdbcTemplate.query(ConstantUtils.ORACLE_GET_UPS_WAREHOUSE,
					new RowMapper<UpsWarehouse>() {

						@Override
						public UpsWarehouse mapRow(ResultSet rs, int rowNum) throws SQLException {

							UpsWarehouse upsWarehouse = new UpsWarehouse();

							upsWarehouse
									.setBj_date_of_cal_store(rs.getString("bj_date_of_cal_store"));
							upsWarehouse.setCity(rs.getString("city"));
							upsWarehouse.setCity_code(rs.getString("city_code"));
							upsWarehouse.setCode(rs.getString("code"));
							upsWarehouse.setCountry_code(rs.getString("country_code"));
							upsWarehouse.setCountry(rs.getString("country"));
							upsWarehouse.setIs_active(rs.getString("is_active"));
							upsWarehouse.setCreated(DateUtil
									.convertSqlTimestampToUtilDate(rs.getTimestamp("created")));
							upsWarehouse.setCreatedBy(rs.getString("createdBy"));
							upsWarehouse.setName(rs.getString("name"));
							upsWarehouse.setNewtime_zone(rs.getString("newtime_zone"));
							upsWarehouse.setOrg_id(rs.getLong("org_id"));
							upsWarehouse.setRegion_code(rs.getString("region_code"));
							upsWarehouse.setRegion_name(rs.getString("region_name"));
							upsWarehouse.setSales_days(rs.getLong("sales_days"));
							upsWarehouse.setState(rs.getString("state"));
							upsWarehouse.setUpdated(DateUtil
									.convertSqlTimestampToUtilDate(rs.getTimestamp("updated")));
							upsWarehouse.setUpdatedby(rs.getString("updatedby"));
							upsWarehouse.setWarehouse_id(rs.getLong("warehouse_id"));
							upsWarehouse.setWarehouse_group(rs.getString("warehouse_group"));

							return upsWarehouse;
						}
					});
		} else {
			return mongoTemplate.findAll(UpsWarehouse.class);
		}

	}

}
